/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.balysv.materialripple.demo.slice;

import static ohos.agp.components.Component.AXIS_Y;

import com.balysv.materialripple.demo.ResourceTable;
import com.balysv.materialripple.demo.provider.ListCodeProvider;
import com.balysv.materialripple.demo.util.CustomToolbar;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.ListContainer;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.UUID;

public class RecyclerViewAbilitySlice extends AbilitySlice {
    private static final String[] data;

    static {
        data = new String[50];
        for (int i = 0; i < data.length; i++) {
            data[i] = UUID.randomUUID().toString();
        }
    }

    private EventHandler handler;
    private HandlerRunClick mHandlerRunClick = new HandlerRunClick();
    private DelayLongRunAnimator delayLongRunAnimator = new DelayLongRunAnimator();
    private CustomToolbar customToolbar;
    private ListContainer listContainerCode;
    private ListCodeProvider listCodeProvider;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_code_ripple);
        handler = new EventHandler(EventRunner.getMainEventRunner());
        initView();
    }

    private void initView() {
        customToolbar = (CustomToolbar) findComponentById(ResourceTable.Id_toolbar_view);
        listContainerCode = (ListContainer) findComponentById(ResourceTable.Id_list_code_ripple);
        listContainerCode.setScrollbarColor(Color.DKGRAY);
        listContainerCode.setScrollbarThickness(20);
        listContainerCode.setScrollbarFadingEnabled(true);
        listContainerCode.enableScrollBar(AXIS_Y, true);
        listContainerCode.setScrollbarRoundRect(true);
        listContainerCode.setScrollbarRadius(10);
        listCodeProvider = new ListCodeProvider(this, data);
        listContainerCode.setItemProvider(listCodeProvider);
        listContainerCode.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                mHandlerRunClick.setPosition(i);
                handler.postTask(mHandlerRunClick, 400);
            }
        });

        listContainerCode.setItemLongClickedListener(new ListContainer.ItemLongClickedListener() {
            @Override
            public boolean onItemLongClicked(ListContainer listContainer, Component component, int i, long l) {
                delayLongRunAnimator.setPosition(i);
                handler.postTask(delayLongRunAnimator, 400);
                return true;
            }
        });
        customToolbar.getTextRecyclerView().setText("BUTTONS");
        customToolbar.getTextListView().setText("LISTVIEW");
        customToolbar.getTextRecyclerView().setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.balysv.materialripple.demo")
                        .withAbilityName("com.balysv.materialripple.demo.MainAbility")
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
                terminateAbility();
            }
        });

        customToolbar.getTextListView().setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.balysv.materialripple.demo")
                        .withAbilityName("com.balysv.materialripple.demo.ListViewAbility")
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
                terminateAbility();
            }
        });
    }

    /**
     * 点击事件延时显示，用以解决不知道什么原因的toast和数值动画冲突
     */
    private class HandlerRunClick implements Runnable {
        private int position;

        public void setPosition(int position) {
            this.position = position;
        }

        @Override
        public void run() {
            new ToastDialog(RecyclerViewAbilitySlice.this).setContentText("Rippled item:   " + position).setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                    .setAlignment(LayoutAlignment.BOTTOM | LayoutAlignment.HORIZONTAL_CENTER)
                    .setOffset(0, 60).show();
        }
    }

    private class DelayLongRunAnimator implements Runnable {
        private int position;

        public void setPosition(int position) {
            this.position = position;
        }

        @Override
        public void run() {
            new ToastDialog(RecyclerViewAbilitySlice.this).setContentText("long item: " + position + " and not consumed").setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                    .setAlignment(LayoutAlignment.BOTTOM | LayoutAlignment.HORIZONTAL_CENTER)
                    .setOffset(0, 60).show();
        }
    }
}
